package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;


import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.json.CoorCustomerJson;
import com.example.yiwei.crm.json.contactJson;
import com.example.yiwei.crm.json.customerJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.Class.listviewClass.SimpleCoorCustomerAdapter;
import com.example.yiwei.crm.view.activity.JYCoordingActivity.CoorDetailedCustomerActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class SimpleCoorCustomerActivity extends AppCompatActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{
    private AutoListView listView;
    private SimpleCoorCustomerAdapter adapter;
    private List<CustomerMessageEntity> list;
    private List<CustomerMessageEntity> alist;
    private CustomerMessageEntity customer;
    ImageView img_search;
    private int dock_id;
    private SharedPreferences sp;
    private ImageView img_find,img_luru,img_me,img_tj,img_back;
    int page = 1;
    int a=1;
    private ImageView img_shouye,img_more1;
    private PopupWindow mPopWindow;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<CustomerMessageEntity> result = (List<CustomerMessageEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    listView.onRefreshComplete();
                    if (list!=null){
                        list.clear();
                    }
                    if (list!=null){
                        list.addAll(result);
                    }else {
                        list = result;
                    }
                    if (a==1){
                       adapter = new SimpleCoorCustomerAdapter(SimpleCoorCustomerActivity.this,list);
                        listView.setAdapter(adapter);
                        a++;
                    }

                    break;
                case AutoListView.LOAD:
                    listView.onLoadComplete();
                    if (result!=null){
                        list.addAll(result);
                    }

                    break;
            }
            if (result!=null) {
//                listView.setResultSize(result.size());
            }else{
//                listView.setResultSize(0);
            }
            adapter.notifyDataSetChanged();

        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_coor_customer);
        listView= (AutoListView) findViewById(R.id.coor_custmer_alv);
        img_search= (ImageView) findViewById(R.id.customerSimpleMessage_btn_search);
        listView.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        img_back= (ImageView) findViewById(R.id.detailed_doc_img_back);
        img_find= (ImageView) findViewById(R.id.item_image_cx);
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        listView.setDividerHeight(1);
        listView.setOnRefreshListener(this);
        listView.setOnLoadListener(this);
        dock_id = getIntent().getExtras().getInt("dockid");
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                        if(getIntent().getExtras().getInt("KXA01")!=0){
                            object.put("KXA01",getIntent().getExtras().getInt("KXA01"));
                        }
                    if (getIntent().getIntExtra("KAA09",0)!=0){
                        object.put("KAA09",getIntent().getIntExtra("KAA09",0));
                    }
                    if (getIntent().getIntExtra("KAA04",0)!=0){
                        object.put("KAA04",getIntent().getIntExtra("KAA04",0));
                    }
                    if (!"".equals(getIntent().getStringExtra("KAA02"))){
                        object.put("KAA02",getIntent().getStringExtra("KAA02"));
                    }
                    if (!"".equals(getIntent().getStringExtra("KLA06"))){
                        object.put("KLA06",getIntent().getStringExtra("KLA06"));
                    }
                     if (!"".equals(getIntent().getStringExtra("CTS01A"))){
                         object.put("CTS01A",getIntent().getStringExtra("CTS01A"));
                     }
                    if (!"".equals(getIntent().getStringExtra("CTS01B"))){
                        object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                Log.d("aaa",json);
                String result = NetWorkUtils.getCount(baseClass.find_coor_allcustomer_URL+page,json);
                list = CoorCustomerJson.jsonVisitMessage(result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (list!=null){
                            adapter = new SimpleCoorCustomerAdapter(SimpleCoorCustomerActivity.this,list);
                            listView.setAdapter(adapter);
                        }else{
                            Toast.makeText(SimpleCoorCustomerActivity.this, "该条件下未搜索到信息", Toast.LENGTH_SHORT).show();
                        }
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        JSONObject object1 = new JSONObject();
                                        try {
                                            object1.put("KAA01",list.get(position-1).getClient_id());
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        GetCustumer.create();
                                        GetContact.create();
                                        String json = object1.toString();
                                        String reult = NetWorkUtils.getCount(baseClass.find_customer_msg, json);
                                        GetCustumer.customers = customerJson.jsonCustomerDetailedMessage1(reult,null);
                                        GetContact.contact = contactJson.detailedContactMessageJson1(reult,null);
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                if (GetCustumer.customers!=null&& GetContact.contact!=null){
                                                    Intent intent = new Intent(SimpleCoorCustomerActivity.this,CoorDetailedCustomerActivity.class);
                                                    startActivity(intent);
                                                }

                                            }
                                        });
                                    }
                                }).start();
                            }
                        });
                    }
                });
            }
        }).start();
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SimpleCoorCustomerActivity.this, JDcoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
        img_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SimpleCoorCustomerActivity.this, JDcoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SimpleCoorCustomerActivity.this, JDcoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SimpleCoorCustomerActivity.this, JDcoorActivity.class);
                intent.putExtra("tt","tt");
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            page=page+1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
                if(getIntent().getExtras().getInt("KXA01")!=0){
                    object.put("KXA01",getIntent().getExtras().getInt("KXA01"));
                }
                if (getIntent().getExtras().getInt("KAA09")!=0){
                    object.put("KAA09",getIntent().getExtras().getInt("KAA09"));
                }
                if (!"".equals(getIntent().getStringExtra("KAA04"))){
                    object.put("KAA04",getIntent().getStringExtra("KAA04"));
                }
                if (!"".equals(getIntent().getStringExtra("KAA02"))){
                    object.put("KAA02",getIntent().getStringExtra("KAA02"));
                }
                if (!"".equals(getIntent().getStringExtra("KAA06"))){
                    object.put("KAA06",getIntent().getStringExtra("KAA06"));
                }
                if (!"".equals(getIntent().getStringExtra("CTS01A"))){
                    object.put("CTS01A",getIntent().getStringExtra("CTS01A"));
                }
                if (!"".equals(getIntent().getStringExtra("CTS01B"))){
                    object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            String result = NetWorkUtils.getCount(baseClass.find_coor_allcustomer_URL+page,json);
            alist = CoorCustomerJson.jsonVisitMessage(result);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
            if (alist!=null){
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                msg.obj = alist;
                handler.sendMessage(msg);
                Toast.makeText(SimpleCoorCustomerActivity.this, "无更多数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            page =1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
                if(getIntent().getExtras().getInt("KXA01")!=0){
                    object.put("KXA01",getIntent().getExtras().getInt("KXA01"));
                }
                if (getIntent().getExtras().getInt("KAA09")!=0){
                    object.put("KAA09",getIntent().getExtras().getInt("KAA09"));
                }
                if (!"".equals(getIntent().getStringExtra("KAA04"))){
                    object.put("KAA04",getIntent().getStringExtra("KAA04"));
                }
                if (!"".equals(getIntent().getStringExtra("KAA02"))){
                    object.put("KAA02",getIntent().getStringExtra("KAA02"));
                }
                if (!"".equals(getIntent().getStringExtra("KAA06"))){
                    object.put("KAA06",getIntent().getStringExtra("KAA06"));
                }
                if (!"".equals(getIntent().getStringExtra("CTS01A"))){
                    object.put("CTS01A",getIntent().getStringExtra("CTS01A"));
                }
                if (!"".equals(getIntent().getStringExtra("CTS01B"))){
                    object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            String result = NetWorkUtils.getCount(baseClass.find_coor_allcustomer_URL+1,json);
            alist = CoorCustomerJson.jsonVisitMessage(result);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (alist!=null){
                listView.setVisibility(View.VISIBLE);
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                listView.setVisibility(View.GONE);
                Toast.makeText(SimpleCoorCustomerActivity.this, "无数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
            UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
            LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        onRefresh();
    }
}
